package wiiusej.apptest;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.BlockingQueue;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import wiiusej.WiiUseApiManager;
import wiiusej.Wiimote;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteBasicListener;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;
import wiiusej.wiiusejevents.wiiuseapievents.WiiUseApiEvent;

public class WiimoteNetListener extends Thread{

	
	private static Logger fileLogger;
	//private static Object[] myWiimotes = new ArrayList<Object>();
	
	BlockingQueue<WiiUseApiEvent> fromPostmant;
	WiiUseApiManager manager;
	
	private static HashMap<Integer, Wiimote> myWiimotes;	
	private ArrayList<Long> timeStats = new ArrayList<Long>();
	
	private final long packetLimit = 10000;
	private long lastRefNumber = 0;
	private int sendCounter = 0;
	
	
	public WiimoteNetListener(WiiUseApiManager manager, BlockingQueue<WiiUseApiEvent> fromPostman, 
			HashMap<Integer, Wiimote> wiimotes){		
			
		this.fromPostmant = fromPostman;
		this.manager = manager;
		this.myWiimotes = wiimotes;
				
		PropertyConfigurator.configure("log4j.properties");
		this.fileLogger = Logger.getLogger("netListener");
		
		Set set = wiimotes.keySet();	
		//fileLogger.info("Wiimotes: " + set.toString());
		//fileLogger.info("");
		
		run();
	}
	
	public void run(){				
		
		while(true){ 			
			
			SimpleDateFormat tsFormat = new SimpleDateFormat("mm:ss:SS");
			
			while(packetLimit>lastRefNumber-1){
				sendCounter++;
				fileLogger.info("**************************************************");
				Timestamp ts1 = new Timestamp (System.currentTimeMillis());
				fileLogger.info("Requesting Status " + tsFormat.format(ts1) + " sendCounter: " + sendCounter );
				sendStatusPacket();
							
				try {				
					WiiUseApiEvent evt = (WiiUseApiEvent) fromPostmant.take();
					
					if(evt == null){
						fileLogger.info("NULL elemeent retreived");						
					}
					
					Timestamp ts2 = new Timestamp (System.currentTimeMillis());
					fileLogger.info("Response from Wiimote " + tsFormat.format(ts2) + " refNumber: " + evt.getRefNumber());
					//fileLogger.info("delayreceived");
					delayCalc(ts1, ts2);
					
					lastRefNumber = evt.getRefNumber();
					
					//fileLogger.info(newEvent);
					
				} catch (InterruptedException e) {
					e.printStackTrace();
				}				
			}			
		}
	}	
	
	
	private void delayCalc(Timestamp ts1, Timestamp ts2) {		
		Long delay = ts2.getTime() - ts1.getTime();
		fileLogger.info(delay);
		
		//timeStats.add(delay);		
	}

	private void sendStatusPacket() {		
		/*
		Set s=wiimotes.keySet();  
		Iterator i=s.iterator();  
		while(i.hasNext)  
		S.o.p(i.next());*/
		
		//fileLogger.info("...Sending Status Packet...");
		manager.getStatus(1);
		
		/*
		Set set = myWiimotes.keySet();		
		Iterator it = set.iterator();
		
		Integer id = 0; 
		
		for(int i = 0; i<1; i++){			
			id = (Integer) it.next();			
		}
		
		if(!myWiimotes.isEmpty()){		
			manager.getStatus(11);			
		}else{
			fileLogger.info("WARNING: There are no Wiimotes!!");
		}*/
	}

//	public ArrayList<Integer> getMyWiimotes() {
//		return myWiimotes;
//	}
//
//	public void setMyWiimotes(ArrayList<Integer> myWiimotes) {
//		this.myWiimotes = myWiimotes;
//	}
//	
//	
//	public void addMyWiimotes(Integer id) {
//		this.myWiimotes.add(id);
//	}

	

	

	
}
